package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.OrderStatusEnum;
import com.jimmy.medical.model.SignInfo;
import com.jimmy.medical.service.IServiceFlowOrderService;
import com.jimmy.medical.util.CommonUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
@Qualifier("serviceFlowOrderService")
public class ServiceFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements IServiceFlowOrderService {


    @Override
    public Long saveSimpleProcess(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "初步处理", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveServiceSignInfo(SignInfo info) {
        FlowOrder flowOrder = repository.findOne(info.getOrderId());

        User user = userRepository.findOne(info.getUserId());
        Order order = flowOrder.getOrder();
        order.setUpdateTime(CommonUtils.getNowTimeShortString());
        order.setUpdateUser(user.getRealname());
        order.setStatus(OrderStatusEnum.EXECUTED.getKey() + "");
        orderRepository.save(order);

        OrderExecutionRecord record = new OrderExecutionRecord();
        record.setCheckInfo(info.getServiceContent());
        record.setCreateTime(CommonUtils.getNowTimeShortString());
        record.setCreateUser(user.getRealname());

        OrderHistory history = new OrderHistory("服务办理", "修改订单状态为已执行(50)", order, user.getRealname(), CommonUtils.getNowTimeShortString());
        orderHistoryRepository.save(history);

        return saveExecutionRecord(record, new ArrayList<OrderExceptionData>(), new ArrayList<OrderDiagnose>(), new ArrayList<OrderMedicine>() , 2, true);
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信告知", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveFeeProcess(Long flowOrderId, Long userId) {
        return saveFeeProcess(flowOrderId, userId, 5);
    }

    @Override
    public FlowOrder submitService(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成办理", 6);
        return getNextFlowOrder(flowOrder);
    }
}